//
//  ListItem.m
//  kankanVideo
//
//  Created by wangbo on 5/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ListItem.h"
#import "MyRequest.h"
#import "ASIHTTPRequest.h"

@implementation ListItem
@synthesize image;
@synthesize classid;
@synthesize btn;
@synthesize delegate;
@synthesize title;
@synthesize imgURL;

-(id)initWithFrame:(CGRect)frame withParam:(NSMutableArray *)param{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    //NSLog(@"init_listitem_classid:%@",[param objectAtIndex:0]);
    [self setBackgroundColor:[UIColor whiteColor]];
    
    //NSLog(@"%@",[param objectAtIndex:1]);
    
    self.title=[param objectAtIndex:1];
    self.imgURL=[param objectAtIndex:3];
    self.classid=[param objectAtIndex:0];
    self.btn=[UIButton buttonWithType:UIButtonTypeCustom];
    
    
    
    [btn addTarget:self action:@selector(itemTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([imgURL isEqualToString:@"addeditem"]) {
        UIImage *img=[UIImage imageNamed:@"shouye_dingyue.jpg"];
        [btn setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        //[self addSubview:image];
        //[image release];
        [btn setContentMode:UIViewContentModeScaleToFill];
        [btn setImage:img forState:UIControlStateNormal];
        [btn.imageView setContentMode:UIViewContentModeScaleToFill];
        [self addSubview:btn];
    }else if([imgURL isEqualToString:@"aipai"]){
        UIImage *img=[UIImage imageNamed:@"danfan.jpg"];
        [btn setFrame:CGRectMake(1, 1, self.frame.size.width-3, self.frame.size.height-3)];
        //[self addSubview:image];
        //[image release];
        [btn setImage:img forState:UIControlStateNormal];
        [self addSubview:btn];
    }else{
        defaultImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/4, self.frame.size.height/4, self.frame.size.width/2, self.frame.size.height/2)];
        defaultImageView.animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"l1.png"],[UIImage imageNamed:@"l2.png"],[UIImage imageNamed:@"l3.png"],[UIImage imageNamed:@"l4.png"],[UIImage imageNamed:@"l5.png"],[UIImage imageNamed:@"l6.png"],[UIImage imageNamed:@"l7.png"],[UIImage imageNamed:@"l8.png"], nil];
        defaultImageView.animationDuration=1;
        defaultImageView.animationRepeatCount=0;
        [defaultImageView startAnimating];
        [self addSubview:defaultImageView];
        [defaultImageView release];
        
        MyRequest *request=[[[MyRequest alloc]init]autorelease];
        [request loaddataWithURL:imgURL forObject:self];
    }
    return self;
}
-(void)itemTapped:(id)sender{
    [delegate showVideoList:classid withTitle:title];
}
/*-(id)initWithFrame:(CGRect)frame withImageURL:(NSString *)imageURL
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    [self setBackgroundColor:[UIColor grayColor]];
    if ([imageURL isEqualToString:@"addeditem"]) {
        UIImage *img=[UIImage imageNamed:@"additemed.png"];
        self.image=[[UIImageView alloc]initWithImage:img];
        NSLog(@"%f",self.frame.size.width);
        [image setFrame:CGRectMake(1, 1, self.frame.size.width-2, self.frame.size.height-2)];
        [self addSubview:image];
        [image release];
    }else if([imageURL isEqualToString:@"aipai"]){
        UIImage *img=[UIImage imageNamed:@"danfan.jpg"];
        self.image=[[UIImageView alloc]initWithImage:img];
        NSLog(@"%f",self.frame.size.width);
        [image setFrame:CGRectMake(1, 1, self.frame.size.width-2, self.frame.size.height-2)];
        [self addSubview:image];
        [image release];
    }else{
        MyRequest *request=[[[MyRequest alloc]init]autorelease];
        [request loaddataWithURL:imageURL forObject:self];
    }
    return self;
}*/

-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    if(request.responseStatusCode==200){
        UIImage *img=[UIImage imageWithData:request.responseData];
        //self.image=[[UIImageView alloc]initWithImage:img];
        //NSLog(@"width:%f and height:%f",self.frame.size.width-2,self.frame.size.height-2);
        [btn setFrame:CGRectMake(5, 5, self.frame.size.width-10, self.frame.size.height-10)];
        //[self addSubview:image];
        //[image release];
        [btn setImage:img forState:UIControlStateNormal];
        UIView *lblback=[[UIView alloc]initWithFrame:CGRectMake(0, btn.frame.size.height-30, btn.frame.size.width, 30)];
        [lblback setBackgroundColor:[UIColor blackColor]];
        [lblback setAlpha:0.6];
        [btn addSubview:lblback];
        [lblback release];
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, btn.frame.size.height-30, btn.frame.size.width, 30)];
        lbl.text=title;
        [lbl setTextColor:[UIColor whiteColor]];
        [lbl setBackgroundColor:[UIColor clearColor]];
        [lbl setFont:[UIFont fontWithName:@"Arial" size:14]];
        [lbl setTextAlignment:UITextAlignmentCenter];
        [btn addSubview:lbl];
        [lbl release];
        [self addSubview:btn];
        [defaultImageView removeFromSuperview];
    }
}

-(void) dealloc{
    [image release];
    [classid release];
    [title release];
    [btn release];
    [imgURL release];
    delegate=nil;
    [super dealloc];
}
-(void)imageWithURL:(NSString *)url{
    UIImage *img=[UIImage imageNamed:@"123.jpg"];
    image=[[UIImageView alloc]initWithImage:img];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
